/*     */ package com.zimbra.cs.mime.handler;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.convert.AttachmentInfo;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.MimeHandler;
/*     */ import com.zimbra.cs.mime.MimeHandlerException;
/*     */ import com.zimbra.cs.mime.MimeHandlerManager;
/*     */ import java.io.InputStream;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Set;
/*     */ import javax.activation.DataSource;
/*     */ import net.fortuna.ical4j.data.ParserException;
/*     */ import org.apache.lucene.document.Document;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TextCalendarHandler
/*     */   extends MimeHandler
/*     */ {
/*     */   private String mContent;
/*     */   private ZCalendar.ZVCalendar miCalendar;
/*     */   
/*     */   protected boolean runsExternally()
/*     */   {
/*  50 */     return false;
/*     */   }
/*     */   
/*     */   public ZCalendar.ZVCalendar getICalendar() throws MimeHandlerException {
/*  54 */     analyze(true);
/*  55 */     return this.miCalendar;
/*     */   }
/*     */   
/*     */   protected String getContentImpl() throws MimeHandlerException {
/*  59 */     analyze(false);
/*  60 */     return this.mContent;
/*     */   }
/*     */   
/*     */   private void analyze(boolean needCal) throws MimeHandlerException {
/*  64 */     if (this.mContent != null) {
/*  65 */       return;
/*     */     }
/*  67 */     DataSource source = getDataSource();
/*  68 */     if (source == null) {
/*  69 */       this.mContent = "";
/*  70 */       return;
/*     */     }
/*  72 */     InputStream is = null;
/*     */     try {
/*  74 */       is = source.getInputStream();
/*  75 */       String charset = "utf-8";
/*  76 */       String ctStr = source.getContentType();
/*  77 */       if (ctStr != null) {
/*  78 */         String cs = Mime.getCharset(ctStr);
/*  79 */         if (cs != null)
/*  80 */           charset = cs;
/*     */       }
/*  82 */       if (needCal) {
/*  83 */         this.miCalendar = ZCalendar.ZCalendarBuilder.build(is, charset);
/*  84 */         StringBuilder buf = new StringBuilder(1024);
/*  85 */         int maxLength = MimeHandlerManager.getIndexedTextLimit();
/*  86 */         Iterator<ZCalendar.ZComponent> compIter = this.miCalendar.getComponentIterator();
/*  87 */         Iterator<ZCalendar.ZProperty> propIter; while ((compIter.hasNext()) && (buf.length() < maxLength)) {
/*  88 */           ZCalendar.ZComponent comp = (ZCalendar.ZComponent)compIter.next();
/*  89 */           for (propIter = comp.getPropertyIterator(); propIter.hasNext();) {
/*  90 */             ZCalendar.ZProperty prop = (ZCalendar.ZProperty)propIter.next();
/*  91 */             if (sIndexedProps.contains(prop.getName())) {
/*  92 */               String value = prop.getValue();
/*  93 */               if ((value != null) && (value.length() > 0)) {
/*  94 */                 if (buf.length() > 0)
/*  95 */                   buf.append(' ');
/*  96 */                 buf.append(value);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/* 101 */         this.mContent = buf.toString();
/*     */       } else {
/* 103 */         IcsParseHandler handler = new IcsParseHandler();
/* 104 */         ZCalendar.ZCalendarBuilder.parse(is, charset, handler);
/* 105 */         this.mContent = handler.getContent();
/*     */       }
/*     */     } catch (Exception e) {
/* 108 */       this.mContent = "";
/* 109 */       ZimbraLog.index.warn("error reading text/calendar mime part", e);
/* 110 */       throw new MimeHandlerException(e);
/*     */     } finally {
/* 112 */       ByteUtil.closeStream(is);
/*     */     }
/*     */   }
/*     */   
/*     */   public void addFields(Document doc) {}
/*     */   
/*     */   public String convert(AttachmentInfo doc, String baseURL)
/*     */   {
/* 120 */     throw new UnsupportedOperationException();
/*     */   }
/*     */   
/*     */   public boolean doConversion() {
/* 124 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/* 129 */   private static final Set<String> sIndexedProps = new HashSet();
/* 130 */   static { sIndexedProps.add(ZCalendar.ICalTok.SUMMARY.toString());
/* 131 */     sIndexedProps.add(ZCalendar.ICalTok.DESCRIPTION.toString());
/* 132 */     sIndexedProps.add(ZCalendar.ICalTok.COMMENT.toString());
/* 133 */     sIndexedProps.add(ZCalendar.ICalTok.LOCATION.toString());
/*     */   }
/*     */   
/*     */   public class IcsParseHandler implements ZCalendar.ZICalendarParseHandler
/*     */   {
/*     */     private StringBuilder mContentBuf;
/*     */     private int mMaxLength;
/*     */     private boolean mMaxedOut;
/*     */     private String mCurProp;
/*     */     private int mNumCals;
/*     */     private boolean mInZCalendar;
/*     */     
/*     */     public IcsParseHandler()
/*     */     {
/* 147 */       this.mContentBuf = new StringBuilder(1024);
/* 148 */       this.mMaxLength = MimeHandlerManager.getIndexedTextLimit();
/*     */     }
/*     */     
/*     */     public String getContent() {
/* 152 */       return this.mContentBuf.toString();
/*     */     }
/*     */     
/*     */     private void appendContent(String str) {
/* 156 */       if ((str != null) && (str.length() > 0)) {
/* 157 */         if (this.mContentBuf.length() > 0)
/* 158 */           this.mContentBuf.append(' ');
/* 159 */         this.mContentBuf.append(str);
/* 160 */         this.mMaxedOut = (this.mContentBuf.length() >= this.mMaxLength);
/*     */       }
/*     */     }
/*     */     
/*     */     public void startCalendar() throws ParserException
/*     */     {
/* 166 */       this.mInZCalendar = true;
/*     */     }
/*     */     
/*     */     public void endCalendar() throws ParserException
/*     */     {
/* 171 */       this.mInZCalendar = false;
/* 172 */       this.mNumCals += 1;
/*     */     }
/*     */     
/*     */ 
/* 176 */     public boolean inZCalendar() { return this.mInZCalendar; }
/*     */     
/* 178 */     public int getNumCals() { return this.mNumCals; }
/*     */     
/*     */ 
/*     */ 
/*     */     public void startComponent(String name) {}
/*     */     
/*     */ 
/*     */     public void endComponent(String name)
/*     */       throws ParserException
/*     */     {}
/*     */     
/*     */ 
/*     */     public void startProperty(String name)
/*     */     {
/* 192 */       this.mCurProp = (name != null ? name.toUpperCase() : null);
/*     */     }
/*     */     
/*     */     public void propertyValue(String value) throws ParserException
/*     */     {
/* 197 */       if ((!this.mMaxedOut) && (TextCalendarHandler.sIndexedProps.contains(this.mCurProp))) {
/* 198 */         appendContent(value);
/*     */       }
/*     */     }
/*     */     
/*     */     public void endProperty(String name)
/*     */     {
/* 204 */       this.mCurProp = null;
/*     */     }
/*     */     
/*     */     public void parameter(String name, String value) {}
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/handler/TextCalendarHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */